
/* JavaScript content from map/classes/Utils.js in folder common */
define('map/classes/Utils',['dojo'],function(){
  
  return { _fromProjection:  new OpenLayers.Projection("EPSG:4326"),
    _toProjection: new OpenLayers.Projection("EPSG:900913"),
    toOL: function(xy) {
      var result = xy;
      return result.transform(this._fromProjection, this._toProjection);
    },
    toLL: function(xy) {
      var result = xy;
      return result.transform(this._toProjection, this._fromProjection);
    },
   
    getCurrentTime: function() { 
      var d = new Date()
        , ms = d.getMilliseconds()
        , mstr = ms;

      if (ms < 100 && ms >= 10) {
        mstr = "0" + ms;
      } else if (ms < 10) {
        mstr = "00" + ms;
      }
      return d.getSeconds() + ":" + mstr; 
    },

    makeId: function() {
      var text = "";
      var allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
      for (var i = 0; i < 10; i++) {
        text += allowed.charAt(Math.floor(Math.random() * allowed.length));
      }
      return text;
    },

    isLeft: function(start, end, pt) {
      var ret = ((end.x - start.x)*(start.y - pt.y) - (start.y - end.y)*(pt.x - start.x)) > 0;
      return ret;
    },
    findSide: function(start, end, pt, debug) {
      var nearlyEqual = function(a, b) {
        return (Math.abs(b - a) < 0.01);
      };

      if (nearlyEqual(end.x - start.x, 0)) { // vertical line
        if (pt.x < end.x) { return end.y > start.y ? 1 : -1; }
        if (pt.x > end.x) { return end.y > start.y ? -1 : 1; }
        return 0;
      }
      if (nearlyEqual(end.x - start.x, 0)) { // horizontal line
        if (pt.y < end.y) { return end.x > start.x ? 1 : -1; }
        if (pt.y > end.y) { return end.x > start.x ? -1 : 1; }
        return 0;
      }
      var slope = (end.y - start.y) / (end.x - start.x);
      var yIntercept = start.y - start.x * slope;
      var cSolution = (slope * pt.x) + yIntercept;
      if (slope !== 0) {
        if (pt.y > cSolution) { return end.x > start.x ? 1 : -1; }
        if (pt.y < cSolution) { return end.x > start.x ? -1 : 1; }
        return 0;
      }
      return 0;
    },
    vectorToRadians : function(vector, precision) {
      if (precision == null) { precision = 3; }
      var rad = (Math.atan(vector.y / vector.x)).toFixed(precision);
      return parseFloat(rad);
    },
    radiansToDegrees:function(rad, precision) {
      if (precision == null) { precision = 1; }
      return (360 * (rad / (2*Math.PI))).toFixed(precision);
    }
  };
});